package com.ctshk.sapp.order.airexpress.controller;


import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.rpc.order.airexpress.dto.AirportExpressOrderDownloadDTO;
import com.ctshk.rpc.order.airexpress.dto.AirportExpressOrderETicketGetDTO;
import com.ctshk.rpc.order.airexpress.dto.AirportExpressOrderReceiptGetDTO;
import com.ctshk.rpc.order.airexpress.req.AirportExpressOrderReceiptDownloadReq;
import com.ctshk.rpc.order.airexpress.req.AirportExpressOrderReceiptInformEmailReq;
import com.ctshk.rpc.order.airexpress.req.AirportExpressOrderReceiptSaveReq;
import com.ctshk.rpc.order.airexpress.service.IAirportExpressOrderReceiptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.ctshk.common.web.support.BaseController;

import java.util.List;

/**
 * <p>
 * 机场快线订单收据 前端控制器
 * </p>
 *
 * @author 叶冠峰
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/airport/express/order/receipt")
@Api(value = "机场快线订单收据", tags = "机场快线订单收据")
public class AirportExpressOrderReceiptController extends BaseController {
    @DubboReference
    IAirportExpressOrderReceiptService airportExpressOrderReceiptService;

    @ApiOperation(value = "机场快线-订单操作-更新收据", notes = "机场快线-订单操作-更新收据(下载，打印时候调用)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-更新收据", operDesc = "机场快线-订单中心-更新收据")
    @PostMapping("/update")
    public Result saveReceipt(@RequestBody AirportExpressOrderReceiptSaveReq airportExpressOrderReceiptSaveReq) {
        Long userId = getCurrentUser(getRequest()).getId();
        int num = airportExpressOrderReceiptService.update(airportExpressOrderReceiptSaveReq, userId);
        return Result.success(num);
    }

    @ApiOperation(value = "机场快线-订单操作-查询收据", notes = "机场快线-订单操作-查询收据")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-查询收据", operDesc = "机场快线-订单中心-查询收据")
    @GetMapping("/{orderId}/receipt")
    public Result<AirportExpressOrderReceiptGetDTO> getReceiptByOrderId(@PathVariable("orderId") Long orderId) {
        AirportExpressOrderReceiptGetDTO airportExpressOrderReceiptGetDTO = airportExpressOrderReceiptService.getReceiptByOrderId(orderId);
        return Result.success(airportExpressOrderReceiptGetDTO);
    }

    @ApiOperation(value = "机场快线-订单操作-查询电子票", notes = "机场快线-订单操作-查询电子票")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-查询电子票", operDesc = "机场快线-订单中心-查询电子票")
    @GetMapping("/{orderId}/e-ticket")
    public Result<List<AirportExpressOrderETicketGetDTO>> getETicketByOrderId(@PathVariable("orderId") Long orderId) {
        List<AirportExpressOrderETicketGetDTO> airportExpressOrderETicketGetDTOList = airportExpressOrderReceiptService.getETicketByOrderId(orderId);
        return Result.success(airportExpressOrderETicketGetDTOList);
    }

    @ApiOperation(value = "机场快线-订单操作-下载文件", notes = "机场快线-订单操作-下载文件")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-下载文件", operDesc = "机场快线-订单中心-下载文件")
    @GetMapping("/{orderId}/{type}/download")
    public ResponseEntity<byte[]> download(@PathVariable("orderId") Long orderId, @PathVariable("type") @ApiParam("类型,receipt:收据,e-ticket:电子票") String type) {
        AirportExpressOrderReceiptDownloadReq airportExpressOrderReceiptDownloadReq = new AirportExpressOrderReceiptDownloadReq(orderId, type);
        AirportExpressOrderDownloadDTO airportExpressOrderDownloadDTO = airportExpressOrderReceiptService.download(airportExpressOrderReceiptDownloadReq);
        byte[] fileBytes = airportExpressOrderDownloadDTO.getFileBytes();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attchement;filename=" + airportExpressOrderDownloadDTO.getFullName());
        HttpStatus statusCode = HttpStatus.OK;
        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(fileBytes, headers, statusCode);
        return entity;
    }

    @ApiOperation(value = "机场快线-订单操作-发送收据邮件", notes = "机场快线-订单操作-发送收据邮件")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-发送收据邮件", operDesc = "机场快线-订单中心-发送收据邮件")
    @GetMapping("/{orderId}/receipt/email")
    public Result sendReceiptByOrderId(@PathVariable("orderId") Long orderId) {
        return airportExpressOrderReceiptService.sendReceiptByOrderId(orderId);
    }

    @ApiOperation(value = "机场快线-订单操作-发送电子票邮件", notes = "机场快线-订单操作-发送电子票邮件")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-发送电子票邮件", operDesc = "机场快线-订单中心-发送电子票邮件")
    @GetMapping("/{orderId}/e-ticket/email")
    public Result sendETicketByOrderId(@PathVariable("orderId") Long orderId) {
        return airportExpressOrderReceiptService.sendETicketByOrderId(orderId);
    }

    @ApiOperation(value = "机场快线-订单操作-通知旅客", notes = "机场快线-订单操作-通知旅客")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "机场快线-订单中心-通知旅客", operDesc = "机场快线-订单中心-通知旅客")
    @PostMapping("/inform")
    public Result sendInformEmailByOrderId(@RequestBody AirportExpressOrderReceiptInformEmailReq airportExpressOrderReceiptInformEmailReq) {
        return airportExpressOrderReceiptService.sendInformByOrderId(airportExpressOrderReceiptInformEmailReq);
    }
}